package com.ruoyi.framework.web.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.UserType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.Arith;
import com.ruoyi.common.utils.Condition;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.vo.SysConfigVO;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wq.domain.WqFriendApply;
import com.ruoyi.wq.domain.WqUserCard;
import com.ruoyi.wq.domain.vo.WqFriendApplyVO;
import com.ruoyi.wq.domain.vo.WqUserCardVO;
import com.ruoyi.wq.service.IWqUserCardService;

@Component
public class UserCardService {
	
	@Autowired
	private IWqUserCardService wqUserCardService;
	
	@Autowired
	private ISysUserService sysUserService;
	
	@Autowired
	private FriendApplyService friendApplyService;
	
	@Autowired
	private AppConfigService appConfigService;

	public WqFriendApplyVO insertWqUserCard(WqUserCardVO wqUserCard){
		WqFriendApplyVO friendApplyVO = null;
		Long applyUserId = SecurityUtils.getUserId();
		SysUser applyUser = sysUserService.getById(applyUserId);
    	if("https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/0".equals(applyUser.getAvatar())){
			throw new ServiceException("请到个人资料上传头像", 877);
		}
    	
    	SysConfigVO config = new SysConfigVO();
    	config.setUserId(applyUserId);
    	config.setScene(wqUserCard.getScene());
    	String value = appConfigService.getCardValue(config);
    	if(StringUtils.isBlank(applyUser.getWechat()) && !"0".equals(value)){
    		throw new ServiceException("请到个人资料完善微信号", 877);
    	}
    	if(StringUtils.isBlank(applyUser.getAlbums())){
			throw new ServiceException("请到个人资料上传相册", 877);
		}
    	if("微信用户".equals(applyUser.getNickName())){
			throw new ServiceException("请到个人资料填写昵称", 877);
		}
		
    	QueryWrapper<WqUserCard> queryWrapper = new QueryWrapper<>();
    	WqUserCard entity = new WqUserCard();
    	entity.setCardUserId(wqUserCard.getCardUserId());
    	entity.setUserId(SecurityUtils.getUserId());
    	queryWrapper.setEntity(entity);
    	WqUserCard uc = wqUserCardService.getOne(queryWrapper);
    	if(uc == null){
    		uc = new WqUserCard();
    		uc.setCardType(wqUserCard.getCardType());
    		uc.setCardUserId(wqUserCard.getCardUserId());
    		uc.setUserId(SecurityUtils.getUserId());
    		wqUserCardService.save(uc);
    		
    		SysUser user = sysUserService.getById(wqUserCard.getCardUserId());
    		Integer voteNum = user.getVoteNum();
    		if("0".equals(wqUserCard.getCardType())){
    			// 判断是否互相喜欢
    			QueryWrapper<WqUserCard> queryWrapper2 = new QueryWrapper<>();
    			WqUserCard entity2 = new WqUserCard();
    	    	entity2.setCardUserId(SecurityUtils.getUserId());
    	    	entity2.setUserId(wqUserCard.getCardUserId());
    	    	entity2.setCardType("0");
    	    	queryWrapper2.setEntity(entity2);
    	    	WqUserCard uc2 = wqUserCardService.getOne(queryWrapper2);
    	    	if(uc2 != null){
    	    		// 互相喜欢创建聊天房间
    	    		WqFriendApply wqFriendApply = new WqFriendApply();
    	    		wqFriendApply.setUserId(wqUserCard.getCardUserId());
    	    		wqFriendApply.setMessage("心动匹配");
    	    		friendApplyVO = friendApplyService.addCardWechat(wqFriendApply);
    	    	}else{
    	    		if(wqUserCard.isFlag()){
    	    			// 互相喜欢创建聊天房间
        	    		WqFriendApply wqFriendApply = new WqFriendApply();
        	    		wqFriendApply.setUserId(wqUserCard.getCardUserId());
        	    		wqFriendApply.setMessage("心动匹配");
        	    		friendApplyVO = friendApplyService.addCardWechat(wqFriendApply);
    	    		}
    	    	}
    			
    			voteNum = voteNum + 1;
    		}else{
    			voteNum = voteNum - 1;
    		}
    		user.setVoteNum(voteNum);
    		sysUserService.updateById(user);
    	}else{
    		if("0".equals(wqUserCard.getCardType())){
    			// 判断是否互相喜欢
    			QueryWrapper<WqUserCard> queryWrapper2 = new QueryWrapper<>();
    			WqUserCard entity2 = new WqUserCard();
    	    	entity2.setCardUserId(SecurityUtils.getUserId());
    	    	entity2.setUserId(wqUserCard.getCardUserId());
    	    	entity2.setCardType("0");
    	    	queryWrapper2.setEntity(entity2);
    	    	WqUserCard uc2 = wqUserCardService.getOne(queryWrapper2);
    	    	if(uc2 != null){
    	    		// 互相喜欢创建聊天房间
    	    		WqFriendApply wqFriendApply = new WqFriendApply();
    	    		wqFriendApply.setUserId(wqUserCard.getCardUserId());
    	    		wqFriendApply.setMessage("心动匹配");
    	    		friendApplyVO = friendApplyService.addCardWechat(wqFriendApply);
    	    	}else{
    	    		if(wqUserCard.isFlag()){
    	    			// 互相喜欢创建聊天房间
        	    		WqFriendApply wqFriendApply = new WqFriendApply();
        	    		wqFriendApply.setUserId(wqUserCard.getCardUserId());
        	    		wqFriendApply.setMessage("心动匹配");
        	    		friendApplyVO = friendApplyService.addCardWechat(wqFriendApply);
    	    		}
    	    	}
    			
    		}
    	}
        return friendApplyVO;
    }
	
	public TableDataInfo getPageListByTopic(SysUser user) {
		int max = 200; // 优质内容
		user.setUserType(UserType.APP.getCode());
		SysUser sysUser = sysUserService.getById(user.getUserId());
		if(StringUtils.isNotBlank(user.getLon()) && StringUtils.isNotBlank(user.getLat())){
			// 更新坐标
			sysUser.setLon(user.getLon());
			sysUser.setLat(user.getLat());
			sysUserService.updateById(sysUser);
		}
		if(sysUser != null){
			if(StringUtils.isBlank(user.getSex())){
				if("1".equals(sysUser.getSex())){
					user.setSex("0");
				}else{
					user.setSex("1");
				}
			}
			user.setLon(sysUser.getLon());
			user.setLat(sysUser.getLat());
		}
		
		user.setAppId(null);
		
		QueryWrapper<WqUserCard> qw = new QueryWrapper<>();
		WqUserCard userCard = new WqUserCard();
		userCard.setUserId(user.getUserId());
		qw.setEntity(userCard);
		Integer count = wqUserCardService.count(qw);
		if(count < max){
			// 前面5条为优质内容
			user.setStatus("3");
		}else{
			// 后面的为用户喜欢的
			user.setStatus("5");
		}
		
		if(StringUtils.isNotBlank(user.getIsCity())){
			if("1".equals(user.getIsCity())){
				// 同城附近
				user.setStatus("6");
			}
		}
		
		
		IPage<SysUser> pageBean = Condition.getPage(user);
		if(sysUser != null){
			pageBean.setSize(30);
		}
		
		/*if("3".equals(user.getStatus())){
			if(count < 5){
				pageBean.setSize(5);
			}else{
				pageBean.setSize(20);
			}
		}*/
		
		if("1".equals(sysUser.getIsManager())){
			if(StringUtils.isNotBlank(user.getIsCity())){
				if("1".equals(user.getIsCity())){
					user.setStatus("3");
				}
			}else{
				user.setStatus("0");
			}
			pageBean = Condition.getPage(user);
			if(sysUser != null){
				pageBean.setSize(100);
			}
		}
		
		IPage<SysUser> page = sysUserService.selectPageListByHot(pageBean, user);
		if(page.getRecords() == null || page.getRecords().size() == 0){
			// 用户喜欢的没有了责重新查询
			user.setStatus("0");
			pageBean = Condition.getPage(user);
			if(sysUser != null){
				pageBean.setSize(30);
			}
			page = sysUserService.selectPageListByHot(pageBean, user);
		}
		
		List<SysUser> emptyList = new ArrayList<SysUser>();
		List<SysUser> userList = new ArrayList<SysUser>();
		if(count > max){
			WqUserCardVO userCardVO = new WqUserCardVO();
			userCardVO.setUserId(user.getUserId());
			userCardVO.setSex(user.getSex());
			userCardVO.setLon(user.getLon());
			userCardVO.setLat(user.getLat());
			userList = wqUserCardService.getCardUserListByUserId(userCardVO);
			for(SysUser e : userList){
				emptyList.add(e);
			}
		}
		
		for(SysUser u : page.getRecords()){
			boolean isExsit = false;
			for(SysUser e : userList){
				if(e.getUserId().equals(u.getUserId())){
					isExsit = true;
					break;
				}
			}
			if(!isExsit){
				emptyList.add(u);
			}
		}
		
		for(SysUser su : emptyList){
			if("0".equals(su.getWechatStatus()) && StringUtils.isNotBlank(su.getWechat())){
				su.setWechatFlag(true);
			}else{
				su.setWechatFlag(false);
			}
			
			if(StringUtils.isNotBlank(su.getInviteCode()) && "1".equals(su.getWechatCheck())){
				su.setInvite(true);
				su.setWechatFlag(true);
			}else{
				su.setInvite(false);
			}
			su.setWechat(null);
			su.setInviteCode(null);
			su.setCoinLevel(StringUtils.getCoinLevel(su.getPayNum()));
			su.setDistance(String.valueOf(Arith.div(su.getMi(), 1000.00, 2)));
		}
		
		return Condition.getTableDataInfo(emptyList, page.getTotal());
	}
	
}
